package resty.tedu.case05;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

/**
 * <pre>
 *  点不到游戏
 *      实现思路:
 *          1.创建一个窗体
 *          2.创建一个画板
 *          3.创建一个按钮
 *          4.按钮添加监听器
 *              按钮监听一个鼠标进入的动作,在进入时进行位置的变量.
 *          5.处理Bug
 *              在按钮位置变幻时,有可能随机出现的位置还在鼠标的范围之内,
 *              所以在画板监听鼠标在移动时产生的位置,赋值给全局变量,在线程中通过判断鼠标是否在按钮内,去解决相应的Bug.
 * </pre>
 *
 * @author : restyhap
 * @date : 2019年03月08日
 */
public class Demo extends JFrame {
    public Demo() {
        this.setTitle("您点不到我");
        this.setSize(800, 600);
        this.setLocation(500, 400);

        DemoPanel dp = new DemoPanel();
        this.add(dp);
        new Thread(dp).start();

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Demo();
    }
}

class DemoPanel extends JPanel implements Runnable {
    JButton jbt;
    Random ran;
    int jbtX;
    int jbtY;
    MouseEvent event;

    public DemoPanel() {
        ran = new Random();
        jbt = new JButton("您点我试试啊");
        this.setLayout(null);

        jbt.setSize(125, 50);
        this.add(jbt);

        jbt.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {

                int x = ran.nextInt(800 - 125 - 18);
                int y = ran.nextInt(600 - 50 - 26);
                jbt.setLocation(x, y);
            }
        });
        this.addMouseMotionListener(new MouseAdapter() {
            /**
             * {@inheritDoc}
             *
             * @param e
             * @since 1.6
             */
            @Override
            public void mouseMoved(MouseEvent e) {
                event = e;
            }
        });

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.setBackground(Color.BLACK);
    }


    public void run() {
        int i = 0;//定义一个数值.
        while (true) {
            if (event != null) {
                i++;//能进来代表event赋值成功
                if (i == 1) {
                    System.out.println("event全局变量赋值成功");//后台打印输出
                }

                /**确定当前按钮的坐标*/
                jbtX = jbt.getLocation().x;
                jbtY = jbt.getLocation().y;
                /**将按钮的坐标通过变量表示出来.*/
                int jLeft = jbtX;
                int jRight = jLeft + 125;
                int jUp = jbtY;
                int jDown = jUp + 50;
                /**将移动的鼠标坐标不停的传进来*/
                int mouseX = event.getX();
                int mouseY = event.getY();

                /**如果满足鼠标的坐标在移动后按钮的范围之内的话,重新给按钮定位*/
                if ((mouseX >= jLeft & mouseX <= jRight) && (mouseY >= jUp & mouseY <= jDown)) {
                    System.out.println("换位了一次");
                    jbtX = ran.nextInt(800 - 125 - 18);
                    jbtY = ran.nextInt(600 - 50 - 26);
                    jbt.setLocation(jbtX, jbtY);
                }
                repaint();//重画
            }
            try {
                Thread.sleep(10);//监听的时间间隔
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}